package com.stylefeng.guns.core.util;

import com.alibaba.druid.sql.visitor.functions.Char;
import com.stylefeng.guns.config.properties.GunsProperties;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.modular.system.model.BusPrdTrace;
import com.stylefeng.guns.modular.system.model.Dept;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

/**
 * 验证码工具类
 */
public class KaptchaUtil {


    /**
     * 获取验证码开关
     */
    public static Boolean getKaptchaOnOff() {
        return SpringContextHolder.getBean(GunsProperties.class).getKaptchaOpen();
    }
    public static String encryptPrdTraceNo(int i,String areaCode,String prdId,String datadate)
    {
        int length=Config.traceNoLength-13-String.valueOf(Integer.toHexString(i)).length();
        String temp=prdId+datadate+ KaptchaUtil.getLengthZero(length)+String.valueOf(Integer.toHexString(i));
        return temp;
    }
    public static String encryptPrdTraceUrl(int i,String areaCode,String prdId,String datadate)
    {
        String temp=Config.qrCodeUrl+encryptPrdTraceNo(i, areaCode, prdId,datadate);
        return temp;
    }
    public static String espectTraceNo(String encryptPrdTraceNo)
    {
        String traceNo="";
        //加密规则实际的数字，转16进制，定长50位，其余补零
        String zeroStr="";
        char[] chars=encryptPrdTraceNo.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if(chars[i]=='0')
            {
                zeroStr+=chars[i];
            }else
            {
                traceNo=encryptPrdTraceNo.substring(i);
                break;
            }
        }
        //转十进制
        traceNo=Integer.parseInt(traceNo)+"";
        return traceNo;
    }
    //方法1：length为产生的位数
    public static String getRandomString(int length){
        //定义一个字符串（A-Z，a-z，0-9）即62位；
        String str="zxcvbnmlkjhgfdsaqwertyuiopQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
        //由Random生成随机数
        Random random=new Random();
        StringBuffer sb=new StringBuffer();
        //长度为几就循环几次
        for(int i=0; i<length; ++i){
            //产生0-61的数字
            int number=random.nextInt(62);
            //将产生的数字通过length次承载到sb中
            sb.append(str.charAt(number));
        }
        //将承载的字符转换成字符串
        return sb.toString();
    }
    /**
     * 第二种方法
     */
    public static String getRandomString2(int length){
        //产生随机数
        Random random=new Random();
        StringBuffer sb=new StringBuffer();
        //循环length次
        for(int i=0; i<length; i++){
            //产生0-2个随机数，既与a-z，A-Z，0-9三种可能
            int number=random.nextInt(3);
            long result=0;
            switch(number){
                //如果number产生的是数字0；
                case 0:
                    //产生A-Z的ASCII码
                    result=Math.round(Math.random()*25+65);
                    //将ASCII码转换成字符
                    sb.append(String.valueOf((char)result));
                    break;
                case 1:
                    //产生a-z的ASCII码
                    result=Math.round(Math.random()*25+97);
                    sb.append(String.valueOf((char)result));
                    break;
                case 2:
                    //产生0-9的数字
                    sb.append(String.valueOf
                            (new Random().nextInt(10)));
                    break;
            }
        }

        return sb.toString();
    }

    //方法1：length为产生的位数
    public static String getLengthZero(int length){

        StringBuffer sb=new StringBuffer();
        //长度为几就循环几次
        for(int i=0; i<length; ++i){

            //将产生的数字通过length次承载到sb中
            sb.append("0");
        }
        //将承载的字符转换成字符串
        return sb.toString();
    }
    public static String getUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }
    public static int getNowDate8()
    {
        Date now = new Date( );
        SimpleDateFormat ft = new SimpleDateFormat ("yyyyMMdd");
        return Integer.parseInt(ft.format(now));
    }
    public static Map comAuth(Map map,Integer comPid,String comId)
    {
        if(comPid==0&&(map.get("comId")==null||map.get("comId")==""))
        {
            map.put("comId","");
        }else  if(comPid!=0&&(map.get("comId")==null||map.get("comId")==""))
        {
            map.put("comId",comId);
        }
        return map;
    }

    public static void main(String[] args) {
        Date now = new Date( );
        SimpleDateFormat ft = new SimpleDateFormat ("yyyyMMdd");
        System.out.println("Current Date: " + ft.format(now));
    }
}